/*
* @Author: dengjiayao
* @Date:   2017-08-29 14:09:25
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-09-18 13:19:07
*/
import { modal } from 'vue-strap'
import { showMsg, saveRecommendFollow } from '../../vuex/action'

export default {
  name: 'recommend-config',
  data() {
    return {
      showModal: false,
      form: {
        type: '-1',
        location: ''
      }
    }
  },
  components: {
    'vue-modal': modal
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xAdd: saveRecommendFollow
    }
  },
  methods: {
    valid() {
      let {type, location} = this.form
      if (+type === -1 || location.length === 0) {
        return Promise.reject({
          error_msg: '*标识的均为必填项'
        })
      } else if (+type === 0 || +type === 2) {
        if (!/\.(hk|sz|sh)+$/i.test(location)) {
          return Promise.reject({
            error_msg: '股票代码应以.HK/.SZ/.SH结尾'
          })
        }
      } else if (+type === 1 || +type === 3) {
        if (isNaN(location)) {
          return Promise.reject({
            error_msg: '组合ID含有非数字字符'
          })
        }
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          let {type, location} = this.form
          let params = {
            type: +type
          }
          switch (+type) {
            case 0:
            case 2:
              params.secuCode = location.toUpperCase()
              break
            case 1:
            case 3:
              params.betaId = +location
            default:
          }
          return this.xAdd(params)
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
